package org.chen.bossTimer.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.chen.bossTimer.config.AppConfig;
import org.chen.bossTimer.response.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api/config")
public class ConfigControl {
	@Autowired
	private AppConfig appConfig;

	private final static Logger logger = LoggerFactory.getLogger(BossControl.class);

	@GetMapping("/tipsTime")
	public ResultData<Map<String, Integer>> getTipsTime(HttpServletRequest req) {
		logger.info("获取tipsTime");
		Map<String, Integer> result = new HashMap<String, Integer>();
		result.put("tipsTime", appConfig.getTipsTime());
		return ResultData.success(result);
	}

	@PutMapping("/tipsTime")
	public ResultData<String> setTipsTime(HttpServletRequest req, @RequestBody Map<String, Integer> params) {
		logger.info("修改tipsTime");
		try {
			Integer tipsTime = params.get("tipsTime");
			if (null != tipsTime) {
				appConfig.setTipsTime(tipsTime);
			}
			return ResultData.success("修改提示时间成功");
		} catch (Exception e) {
			return ResultData.fail(400, "修改提示时间成功");
		}
	}
}
